import cherrypy
import sha

from datetime import *
from app.models import Employee, PayBenefit
from app.controllers.post import PostController
from app.controllers.payroll import PayrollController

from google.appengine.ext import db

class EmployeeController:
	@staticmethod
	def getEmployeesByBenefit(id):
		#benefits = PayBenefit.get_by_id(int(id))
		#print len(benefits)
		return None
	
	@staticmethod
	def getEmployees():
		employees = Employee.all()
		return employees
		
	@staticmethod
	def getEmployee(id):
		employee = Employee.get_by_id(id)
		return employee
		
	@staticmethod
	def deleteEmployee(id):
		employee = EmployeeController.getEmployee(int(id))
		if employee:
			employee.delete()
			
			return True
		else: return False
		
	@staticmethod
	def updateEmployee(uemployee=None):
		if uemployee is not None:
			try:
				employee = EmployeeController.getEmployee(int(uemployee.id))
				employee.surname = uemployee.surname
				employee.othernames = uemployee.othernames
				employee.post = PostController.getPost(int(uemployee.post))
				employee.address1 = uemployee.address1
				employee.address2 = uemployee.address2
				employee.phone1 = uemployee.country_prefix_safcom + uemployee.service_prefix_safcom + uemployee.first_suffix_safcom + uemployee.second_suffix_safcom
				employee.phone2 = uemployee.country_prefix_zain + uemployee.service_prefix_zain + uemployee.first_suffix_zain + uemployee.second_suffix_zain
				employee.email1 = uemployee.email1
				employee.email2 = uemployee.email2
				employee.nssf = uemployee.nssf
				employee.nhif = uemployee.nhif
				employee.pin = uemployee.pin
				employee.gender = uemployee.gender
				employee.country = uemployee.country
				employee.city = uemployee.city
				employee.dob = datetime.date(datetime.strptime(uemployee.dob,"%Y-%m-%d"))
				employee.start = datetime.date(datetime.strptime(uemployee.start,"%Y-%m-%d"))
				employee.end = datetime.date(datetime.strptime(uemployee.end,"%Y-%m-%d"))
				employee.status = uemployee.status
				employee.bankacc = uemployee.bankacc
				employee.active = bool(uemployee.active)
				employee.put()
				
				return True
			except db.TransactionFailedError:
				return False
		else:
			return False
	
	@staticmethod	
	def addEmployee(aemployee=None):
		if aemployee is not None:
			try:
				employee = Employee()
				employee.surname = aemployee.surname
				employee.othernames = aemployee.othernames
				employee.post = PostController.getPost(int(aemployee.post))
				employee.address1 = aemployee.address1
				employee.address2 = aemployee.address2
				employee.phone1 = aemployee.country_prefix_safcom + aemployee.service_prefix_safcom + aemployee.first_suffix_safcom + aemployee.second_suffix_safcom
				employee.phone2 = aemployee.country_prefix_zain + aemployee.service_prefix_zain + aemployee.first_suffix_zain + aemployee.second_suffix_zain
				employee.email1 = aemployee.email1
				employee.email2 = aemployee.email2
				employee.nssf = aemployee.nssf
				employee.nhif = aemployee.nhif
				employee.pin = aemployee.pin
				employee.gender = aemployee.gender
				employee.country = aemployee.country
				employee.city = aemployee.city
				employee.dob = datetime.date(datetime.strptime(aemployee.dob,"%Y-%m-%d"))
				employee.start = datetime.date(datetime.strptime(aemployee.start,"%Y-%m-%d"))
				employee.end = datetime.date(datetime.strptime(aemployee.end,"%Y-%m-%d"))
				employee.status = aemployee.status
				employee.bankacc = aemployee.bankacc
				employee.active = bool(aemployee.active)
				employee.put()

				return True
			except db.TransactionFailedError:
				return False
		else:
			return False
